package cn.glinks.liuhe.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.glinks.liuhe.R;
import cn.glinks.liuhe.api.ApiConstants;
import cn.glinks.liuhe.api.ApiRequest;
import cn.glinks.liuhe.api.jsonbean.CommentHistory;
import cn.glinks.liuhe.api.jsonbean.UserInfo;
import cn.glinks.liuhe.app.AppApplication;
import cn.glinks.liuhe.base.BaseActivity;
import cn.glinks.liuhe.baserx.RxSubscriber;
import cn.glinks.liuhe.utils.ActTransAnimUtils;
import cn.glinks.liuhe.utils.GsonUtils;
import cn.glinks.liuhe.utils.TimeTransformUtils;
import cn.glinks.liuhe.utils.ToastUtils;
import cn.glinks.liuhe.widget.timeline.TimeLineAdapter;
import cn.glinks.liuhe.widget.timeline.TimeLineModel;


/**
 * Des:历史测评页面
 *
 * @author MarsBlade of Cyh
 * @created 2017-03-21 14:43
 */
public class HistoryActivity extends BaseActivity {

    @BindView(R.id.tv_title)
    TextView mTitleText;

    @BindView(R.id.rv_history)
    RecyclerView mHistoryView;

    // 变量
    private UserInfo mUserInfo;
    private TimeLineAdapter mTimeLineAdapter;
    private List<TimeLineModel> mDataList = new ArrayList<>();

    @Override
    public int getLayoutId() {
        return R.layout.activity_history;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        // 加载用户信息
        mUserInfo = ((AppApplication) getApplicationContext()).getLoginUser();
        // 设置为透明状态栏
        SetTranslanteBar();
        // 初始化标题
        mTitleText.setText("历史测评");
        // 初始化
        mHistoryView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        mHistoryView.setHasFixedSize(true);
        mTimeLineAdapter = new TimeLineAdapter(mDataList);
        mHistoryView.setAdapter(mTimeLineAdapter);

        // 发送网络请求
        mRxManager.add(ApiRequest.requestGetCommentHistory(mUserInfo.getJwt())
                .subscribe(new RxSubscriber<String>(mContext) {
                    @Override
                    protected void _onNext(String msg) {
                        if (GsonUtils.getIntValue(msg, "error") == 0) {
                            //ToastUtils.showShort("获取测评历史成功");
                            // 解析响应
                            CommentHistory history = (CommentHistory) GsonUtils.fromJson(msg, CommentHistory.class);
                            int size = history.getResult().size();
                            if (size > 0) {
                                // 清空数据
                                mDataList.clear();
                                // 加载数据
                                for (int i = 0; i < size; i++) {
                                    TimeLineModel time = new TimeLineModel();
                                    String comment = history.getResult().get(i).getComment().getComment();
                                    String advice = history.getResult().get(i).getComment().getAdvice();
                                    String createAt = history.getResult().get(i).getCreatedAt();
                                    Date date = TimeTransformUtils.fromISODate(createAt);
                                    // 添加子项数据
                                    time.setComment(comment);
                                    time.setAdvice(advice);
                                    time.setDate(date);
                                    mDataList.add(time);
                                }
                                // 通知更新
                                mTimeLineAdapter.notifyDataSetChanged();
                            } else {

                            }
                        } else {
                            String reason = GsonUtils.getValue(msg, "reason");
                            ApiConstants.showError(reason);
                        }
                    }

                    @Override
                    protected void _onError(String message) {
                        // TODO:处理失败响应
                        ToastUtils.showShort("获取测评历史失败:" + message);
                    }
                }));
    }

    /*--------------------------------------------------------------------------------
     *                          点击事件接口实现
     ---------------------------------------------------------------------------------*/
    // 返回
    @OnClick(R.id.btn_back)
    public void clickBack() {
        ActTransAnimUtils.finishActionSlideLeftToRight(HistoryActivity.this);
    }

}
